package com.doge.service.wx.submit;

import com.doge.bean.bo.submit.SubmitBo;
import com.doge.bean.pojo.feedback.Feedback;
import com.doge.bean.pojo.user.User;
import com.doge.exception.Param401Exception;
import com.doge.mapper.FeedbackMapper;
import com.doge.mapper.UserMapper;
import com.doge.util.auth.GetSubject;
import com.doge.util.valid.ParamValidation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: doge
 * @description: 反馈服务
 * @author: Mr.Jiang
 * @create: 2021-12-02 22:10
 **/
@Service
public class WxSubmitServiceImpl implements WxSubmitService{
    @Autowired
    FeedbackMapper feedbackMapper;
    @Autowired
    UserMapper userMapper;

    @SneakyThrows
    @Override
    public void submit(SubmitBo submitBo) {
        boolean b = ParamValidation.phoneNumValId(submitBo.getMobile());
        if(!b){
            throw new Param401Exception("参数不对");
        }
        Feedback feedback = new Feedback();
        feedback.setUserId(GetSubject.getUserId());
        User user = userMapper.selectByPrimaryKey(GetSubject.getUserId());
        feedback.setUsername(user.getUsername());
        feedback.setMobile(submitBo.getMobile());
        feedback.setFeedType(submitBo.getFeedType());
        feedback.setContent(submitBo.getContent());
        feedback.setStatus(0);
        feedback.setHasPicture(false);
        if(submitBo.getPicUrls()!=null){
            feedback.setHasPicture(true);
            StringBuffer stringBuffer = new StringBuffer();
            for (String picUrl : submitBo.getPicUrls()) {
                stringBuffer.append(picUrl);
            }
            feedback.setPicUrls(stringBuffer.toString());
        }
        feedback.setAddTime(new Date());
        feedback.setUpdateTime(new Date());
        feedback.setDeleted(false);
        feedbackMapper.insertSelective(feedback);
    }
}
